<template>
	<view class="container">
		<view class="mainBox" v-for="(item,value) in list" :key="value" @click="chooseFn(item.id)">
			<view class="margin-b-xs flex-wrp">
				<view class="margin-r-xs">{{item.userName}}</view>
				<view>{{item.phone}}</view>
				<view @click.stop="toDetailFn(item.id)" class="margin-r-xs" style="margin-left: auto;">编辑</view>
				<view @click.stop="deleteFn(item.id)">删除</view>
			</view>
			<view class="margin-b-xs">{{item.addressDetail}}</view>
			<view class="" @click.stop="openLocation(item.latitude,item.longitude)" v-if="item.latitude&&item.longitude">
				<view class="positioning flex-wrp" :style="'background: url('+imgHead+'card/photo/20250425/1745565492576.png) center center/100% 100% no-repeat;'">
					<view class="text">
						<view class="ov txt11 margin-b-xs" v-if="item.address">{{ item.address }}</view>
						<view class="ov txt22" v-if="item.addressName">{{ item.addressName }}</view>
					</view>
				</view>
			</view>
		</view>
		<view class="card-btn">
			<view class="add-card" @click="addFn">新增联系信息</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				imgHead: 'https://qr-card.oss-accelerate.aliyuncs.com/',
				userInfo:{},
				type:0,
				list:[]
			}
		},
		onLoad(e) {
			this.userInfo = uni.getStorageSync('userInfo')
			if(e.type){
				this.type = e.type
			}
		},
		onShow() {
			this.getListFn()
		},
		methods: {
			getListFn(){
				this.$http('/api/address/list',{pageNo:1,pageSize:999}).then(res=>{
					this.list = res.result.records
				})
			},
			openLocation(latitude,longitude){
				uni.openLocation({
					latitude:Number(latitude),
					longitude:Number(longitude)
				})
			},
			chooseFn(id){
				if(this.type==1){
					this.$http('/api/address/setDefaultAddress',{addressId:id},'POST',1).then(res=>{
						uni.navigateBack()
					})
				}else {
					this.toDetailFn(id)
				}
			},
			toDetailFn(id){
				uni.navigateTo({
					url:'/pages/address/add?id='+id
				})
			},
			deleteFn(id){
				uni.showModal({
					title: '确认删除这条联系信息',
					confirmText:'立即删除',
					success:async(res)=> {
						if (res.confirm) {
							this.$http('/api/address/delete',{
								id:id
							}).then(res=>{
								uni.showToast({
									title:'删除成功',
									icon:'none'
								})
								this.getListFn()
							})
						}
					}
				});
			},
			addFn(){
				uni.navigateTo({
					url:'/pages/address/add'
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.container {
		width: 750rpx;
		min-height: 100vh;
		background: #F0F1F6;
		padding: 1rpx 30rpx 200rpx 30rpx;
		.mainBox {
			margin-top: 20rpx;
			background: #FFF;
			border-radius: 20rpx;
			padding: 20rpx;
			.positioning{
				height: 128rpx;
				padding: 0 24rpx;
				.text{
					width: 532rpx;
					height: 72rpx;
					font-family: Source Han Sans CN, Source Han Sans CN;
					.txt11{
						font-weight: 500;
						font-size: 28rpx;
						color: #333333;
						line-height: 28rpx;
						height: 28rpx;
					}
					.txt22{
						font-weight: 400;
						font-size: 24rpx;
						color: #777777;
						line-height: 24rpx;
						height: 24rpx;
					}
				}
			}
		}
	}
	.margin-b-xs{
		margin-bottom: 20rpx;
	}
	.margin-r-xs {
		margin-right: 20rpx;
	}
	.card-btn{
		width: 100%;
		background: #FFFFFF;
		padding: 20rpx 20rpx 60rpx;
		
		position: fixed;
		bottom: 0;
		left: 0;
		z-index: 99;
		.add-card{
			height: 94rpx;
			background: #005CFF;
			border-radius: 16rpx 16rpx 16rpx 16rpx;
			font-family: Source Han Sans CN, Source Han Sans CN;
			font-weight: bold;
			font-size: 32rpx;
			color: #FFFFFF;
			text-align: center;
			line-height: 94rpx;
			z-index: 99;
		}
	}
</style>
